package com.zhuge_new.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.zhuge_new.Adapter.PlandetailsAdapter;
import com.zhuge_new.R;
import com.zhuge_new.bean.PlandetailsBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.PreferencesUtils;
import com.zhuge_new.untils.StatusBarUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by lenovo on 2018-09-27.
 */

public class PlandetailsActivity extends Activity {


    private Activity context;
    private String repaymentplanId;
    private RecyclerView plan_list;
    private View head;
    private PlandetailsBean plandetailsBean;
    private String RepaymentSum;
    private String cardNo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plandetails);
        Intent intent=getIntent();
        context=this;
        repaymentplanId = intent.getStringExtra("repaymentplanId");
        RepaymentSum=intent.getStringExtra("RepaymentSum");
        cardNo = intent.getStringExtra("cardNo");
        head = View.inflate(context, R.layout.head_plandetails,null);
        initView();
        getData();
    }

    private void initView() {
        StatusBarUtil.setStatusBarColor(PlandetailsActivity.this,R.color.btn_backround);
        plan_list = findViewById(R.id.plan_list);
        plan_list.setLayoutManager(new LinearLayoutManager(this));
        TextView plan_money=head.findViewById(R.id.plan_money);
        plan_money.setText(RepaymentSum);
    }

    public void getData() {
        OkGo.<String>post(BaseHttp.repayment_list)
                .tag(this)
                .headers("token", PreferencesUtils.getString(context,"token"))
                .params("repaymentplanId",repaymentplanId)
                .execute(new StringDialogCallback(context) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj=new JSONObject(response.body());
                            String repayments=obj.getString("repayments");
                            if (repayments.isEmpty()){
                                plandetailsBean=new PlandetailsBean();
                                plandetailsBean.setMsgcode(100);
                            }else {
                                plandetailsBean = (PlandetailsBean) new DataParse().getBean(response.body(),PlandetailsBean.class,0);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        PlandetailsAdapter plandetailsAdapter=new PlandetailsAdapter(context, plandetailsBean,cardNo);
                        plandetailsAdapter.addHeaderView(head);
                        plan_list.setAdapter(plandetailsAdapter);
                    }
                });
    }
}
